﻿var submitStatus = 0;
$.ajaxSetup({
  async: false
  });
function isEmail(theStr) {
	if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(theStr))
		return false;
	return true;
}

function alertTxt(obj,txt) {
	$("#" + obj+"Alert").removeClass("hide");
	$("#" + obj+"Alert").html(txt);
}

function VerifyUserName() {
	var uName = $("#userName").val();
	uName = uName.trim();
	if(uName==""|| uName==null) {
		alertTxt('userName', '用户名不能为空.');
		return false;
	}
	$.getJSON("AjaxForRegWap.aspx", {
        username: escape(uName),
        act: "CheckUserName"
    },function(result) {
		if(result["code"]!="Y") {
			alertTxt('userName', result["msg"]);
			return false;
		}
	    return true;
    });
	return true;
}

function VerifyUserPassword() {
	var reg = /^(([a-z]+[0-9]+)|([0-9]+[a-z]+))[a-z0-9]*$/i;
	var spReg = /^[A-Za-z0-9]+$/;
	var uPassword = $("#userPassword").val();	
	if(uPassword=="6-20字符，数字和字母组合") {
		alertTxt('userPassword', '请输入密码.');
		return false;
	}
	if (uPassword.length > 20) {
		alertTxt('userPassword', '密码太长(最多20个字符).');
		return false;
	}
	if (uPassword.length < 6) {
		alertTxt('userPassword', '密码太短(最少6个字符).');
		return false;
	}

	if (spReg.test(uPassword)) {
		if (!reg.test(uPassword)) {
			alertTxt('userPassword', '密码必须是数字和字母组合.');
			return false;
		}
		return true;
	}
	return true;
}

function VerifyReUserPassword() {
	var uPassword = $("#userPassword").val();
	var rePassword = $("#reUserPassword").val();
	if (uPassword!=rePassword) {
		alertTxt('reUserPassword', '密码确认与密码不符.');
		return false;
	}
	return true;
}

function VerifyUserEmail() {
	var uEmail = $("#userEmail").val();
	uEmail = uEmail.trim();
	if(uEmail==""|| uEmail==null) {
		alertTxt('userEmail', '邮箱不能为空.');
		return false;
	}
	if (!isEmail(uEmail))
	{
		alertTxt('userEmail', '邮件地址有误.');
		return false;
	}
	$.post("AjaxForRegWap.aspx", {
        useremail: escape(uEmail),
        act: "CheckUserEmail"
    },function(result) {
	    //result=$.parseJSON(result);
		if(result["code"]!="Y") {
			alertTxt('userEmail', result["msg"]);
			return false;
		}
	    return true;
    });
	return true;
}

function VerifyVCode() {
	var uVCode = $("#verifyCode").val();
	uVCode = uVCode.trim();
	if(uVCode==""|| uVCode==null) {
		alertTxt('verifyCode', '验证码不能为空.');
		return false;
	}
	return true;
}

function SubmitRegForAndroid() {
	if (!$("#cbHexun").attr("checked")) {
		alertTxt('reg', '必须同意《和讯服务条款》和《版权声明》才能注册.');
		return false;
	} else {
		$("#regAlert").addClass("hide");
	}
	var checkEmail = true;
	var regEmail = "";
	if ($("#userEmail").length > 0) {
		checkEmail = VerifyUserEmail();
		regEmail=$("#userEmail").val();
		regEmail = regEmail.trim();
	}
	
	if (VerifyUserName() && VerifyUserPassword() && VerifyReUserPassword() && checkEmail && VerifyVCode()) {
		var uName = $("#userName").val();
		uName = uName.trim();
		var uPassword = $("#userPassword").val();
		var rePassword = $("#reUserPassword").val();
		var uEmail = regEmail;
		var uVCode = $("#verifyCode").val();
		uVCode = uVCode.trim();
		var regtype = "reguser";
		$.post("AjaxForRegWap.aspx", {
				userName: escape(uName),
				userPassword: escape(uPassword),
				reUserPassword: escape(rePassword),
				userEmail: escape(uEmail),
				verifyCode: escape(uVCode),
				act: regtype
			}, function(result) {
				if(result["code"]=="Y") {
						try {
							window.javatojs.LoginSuccess(uName, result["userid"], result["token"], result["snapcookie"], result["statecookie"], 1);
							return true;
						} catch(e) {
						}
				} else {
					alertTxt('reg', '注册失败.');
					var json = eval(result.options);
					for (var j = 0; j < json.length; j++) {
						if (json[j]["state"] == "N") {
							alertTxt(json[j]["name"], json[j]["msg"]);
						}
					}
					try {
						refreshC_code();
					} catch(e) {

					}
				}
				return true;
			});
	}
	return true;
}

function SubmitRegFrm() {
	if (!$("#cbHexun").attr("checked")) {
		alertTxt('reg', '必须同意《和讯服务条款》和《版权声明》才能注册.');
		return false;
	} else {
		$("#regAlert").addClass("hide");
	}
	var vClient=$("#client").val();
	var checkEmail = true;
	var regEmail = "";
	if ($("#userEmail").length > 0) {
		checkEmail = VerifyUserEmail();
		regEmail=$("#userEmail").val();
		regEmail = regEmail.trim();
	}
	if (VerifyUserName() && VerifyUserPassword() && VerifyReUserPassword() && checkEmail && VerifyVCode()) {
		var uName = $("#userName").val();
		uName = uName.trim();
		var uPassword = $("#userPassword").val();
		var rePassword = $("#reUserPassword").val();
		var uEmail = regEmail;
		var uVCode = $("#verifyCode").val();
		uVCode = uVCode.trim();
		var regtype = "reguser";
		$.post("AjaxForRegWap.aspx", {
				userName: escape(uName),
				userPassword: escape(uPassword),
				reUserPassword: escape(rePassword),
				userEmail: escape(uEmail),
				verifyCode: escape(uVCode),
				act: regtype
			}, function(result) {
				if (result["code"] == "Y") {
					if (vClient == "android") {
						try {
							window.javatojs.LoginSuccess(uName, result["userid"], result["token"], result["snapcookie"], result["statecookie"], 1);
							return true;
						} catch(e) {
						}
					} else if (vClient == "ios") {
						if ($("#regtype").val() == "1") {
							window.location.href = "regSuccessForC.aspx?uname=" + uName + "&email=" + escape(uEmail) + "&uid=" + result["userid"]+ "&snapcookie=" + result["snapcookie"] + "&statecookie=" + result["statecookie"] + "&token="+ result["token"];
						} else {
							window.location.href = "regSuccess.aspx?uname=" + uName + "&email=" + uEmail + "&uid=" + result["userid"]+ "&snapcookie=" + result["snapcookie"] + "&statecookie=" + result["statecookie"] + "&token="+ result["token"];
						}
					} else {
						//HTML5
						window.location.href = "regSuccess.aspx?uname=" + uName + "&email=" + uEmail + "&uid=" + result["userid"]+ "&snapcookie=" + result["snapcookie"] + "&statecookie=" + result["statecookie"] + "&token="+ result["token"];
					}

				} else {
					alertTxt('reg', '注册失败.');
					var json = eval(result.options);
					for (var j = 0; j < json.length; j++) {
						if (json[j]["state"] == "N") {
							alertTxt(json[j]["name"], json[j]["msg"]);
						}
					}
					try {
						refreshC_code();
					} catch(e) {

					}
				}
				return true;
			});
	}
	return true;
}

function linksJump(obj) {
	try {
		var vClient = $("#client").val();
		if (vClient == "android") {
			window.javatojs.LoginMoveTo(obj);
			return true;
		}
	} catch(e) {}
	return true;
}

function SubmitLoginFrm() {
	if ($("#LoginUserName").val().length<1) {
		alertTxt('userName', '用户名不能为空.');
		return false;
	}
	
	if ($("#LoginUserPassword").val().length<1) {
		alertTxt('userPassword', '密码不能为空.');
		return false;
	}
	
	if ($("#LoginUserName").val().length==1) {
		alertTxt('userName', '用户名长度不够.');
		return false;
	}
	
	if ($("#LoginUserPassword").val().length==1) {
		alertTxt('userPassword', '密码长度不够.');
		return false;
	}
	var loginstate = 1;
	if ($("#loginstate").attr("checked") ==true) {
		loginstate = 1;
	} else {
		loginstate = 0;
	}
	if ($("#LoginUserName").val().length>1 && $("#LoginUserPassword").val().length>1) {
		var uName = $("#LoginUserName").val();
		var goUrl = $("#gourl").val();
		uName = uName.trim();
		var uPassword = $("#LoginUserPassword").val();
		$.post("AjaxForRegWap.aspx", {
				username: escape(uName),
				password: escape(uPassword),
				urlReferrerhost: escape(goUrl),
				loginstate: loginstate,
				act: "loginuser"
			}, function(result) {
				if(result["state"]=="Y") {
					try {
						var vClient = $("#client").val();
						if (vClient == "android") {
							window.javatojs.LoginSuccess(uName, result["userid"], result["token"], result["snapcookie"], result["statecookie"], 1);
							return true;
						} else if (vClient == "ios") {
							window.location.href = $("#gourl").val();
							return true;
						} 
					} catch(e) {
					}
				} else {
					alertTxt('login', '登录失败，用户名或密码错误.');
					try {
						var vClient = $("#client").val();
						if (vClient == "android") {
							window.javatojs.LoginSuccess(uName, result["userid"], result["token"], result["snapcookie"], result["statecookie"], 0);
							return true;
						}
					} catch(e) {
					}
				}
				return true;
			});
	}
	return true;
}

function VerifyGetPsdUserName() {
	var uName = $("#getPsdUerName").val();
	uName = uName.trim();
	if(uName==""|| uName==null) {
		alertTxt('userName', '用户名不能为空.');
		return false;
	}
	$.post("AjaxForRegWap.aspx", {
        username: escape(uName),
        act: "CheckUserName"
    },function(result) {
	    //result=$.parseJSON(result);
		if(result["code"]!="N") {
			alertTxt('userName',"用户名不存在");
			return false;
		}
	    return true;
    });
	return true;
}


function SubmitGetPsdFrm() {
	if (VerifyGetPsdUserName() && VerifyVCode()) {

		var uName = $("#getPsdUerName").val();
		uName = uName.trim();
		var vCode = $("#verifyCode").val();

		$.post("AjaxForRegWap.aspx", {
				username: escape(uName),
				vcode: escape(vCode),
				act: "getpsd"
			}, function(result) {
				//result = $.parseJSON(result);
				if (result["code"] == "Y") {
					if($("#regtype").val()=="1") {
						window.location.href = "getpasswordalertforc.aspx?uname=" + escape(uName);
					} else {
						window.location.href = "getpasswordalert.aspx?uname=" + escape(uName);
					}

				} else {
					alertTxt('getPsd',"找回密码失败");
					var json = eval(result.options);
					for (var j = 0; j < json.length; j++) {
						if (json[j]["state"] == "N") {
							alertTxt(json[j]["name"], json[j]["msg"]);
						}
					}
				}
				try {
						refreshC_code();
					}catch(e) {
						
					}
				return true;
			});
		return true;
	} else {
		return false;
	}
}

function SubmitGetPsdFrm2() {
	var uName = $("#getPsdUerName").val();
	uName = uName.trim();
	if(uName.length<1) {
		alertTxt("getPsd","参数不完整，<a href='getpassword.aspx'>重新找回密码</a>");
		return false;
	}
	var eType = $("#emailType").val();
	$.post("AjaxForRegWap.aspx", {
			username: escape(uName),
			vcode: eType,
			act: "getpsd2"
		}, function(result) {
			//result = $.parseJSON(result);
			if (result["code"] == "Y") {
				alertTxt("getPsd","邮件重新发送成功!");
				return true;

			} else {
				alertTxt('getPsd', "找回密码失败，<a href='getpassword.aspx'>重新找回密码</a>");
			}
			return true;
		});
	return true;
}

function SubmitGetPsdFrmForC() {
	var uName = $("#getPsdUerName").val();
	uName = uName.trim();
	if(uName.length<1) {
		alertTxt("getPsd","参数不完整，<a href='getpasswordForClient.aspx'>重新找回密码</a>");
		return false;
	}
	var eType = $("#emailType").val();
	$.post("AjaxForRegWap.aspx", {
			username: escape(uName),
			vcode: eType,
			act: "getpsd2"
		}, function(result) {
			//result = $.parseJSON(result);
			if (result["code"] == "Y") {
				alertTxt("getPsd","邮件重新发送成功!");
				return true;

			} else {
				alertTxt('getPsd', "找回密码失败，<a href='getpasswordForClient.aspx'>重新找回密码</a>");
			}
			return true;
		});
	return true;
}

$(document).ready(function () {
    $("#userName").focus(function () {
	    $("#userNameAlert").addClass("hide");
        $(this).css("color", "#374140");
       
    }).blur(function () {
	    VerifyUserName();
    });
	
	$("#userPassword").focus(function () {
	    $("#userPasswordAlert").addClass("hide");
        $(this).css("color", "#374140");
       
    }).blur(function () {
	    VerifyUserPassword();
    });
	
	$("#reUserPassword").focus(function () {
	    $("#reUserPasswordAlert").addClass("hide");
        $(this).css("color", "#374140");
       
    }).blur(function () {
	    VerifyReUserPassword();
    });
	
	$("#userEmail").focus(function () {
	    $("#userEmailAlert").addClass("hide");
        $(this).css("color", "#374140");
       
    }).blur(function () {
	    VerifyUserEmail();
    });

	$("#verifyCode").focus(function() {
		$("#verifyCodeAlert").addClass("hide");
		$(this).css("color", "#374140");

	})
		.blur(function() {
			VerifyVCode();
		});
	
	$("#cbHexun").click(function () {
		if($("#cbHexun").attr("checked")) {
			$("#regAlert").addClass("hide");
		} else {
			alertTxt('reg', '必须同意《和讯服务条款》和《版权声明》才能注册.');
		}
    });
	
	$("#btn_reg_android").click(function () {
		SubmitRegForAndroid();
    });
	
	$("#btn_reg").click(function () {
		SubmitRegFrm();
    });
	
	$("#LoginUserName").focus(function () {
	    $("#userNameAlert").addClass("hide");
	    $("#loginAlert").addClass("hide");
        $(this).css("color", "#374140");
       
    });
	
	$("#LoginUserPassword").focus(function () {
	    $("#userPasswordAlert").addClass("hide");
		$("#loginAlert").addClass("hide");
        $(this).css("color", "#374140");
       
    });
	
	$("#btn_login").click(function () {
		SubmitLoginFrm();
    });
	
	$("#getPsdUerName").focus(function () {
	    $("#userNameAlert").addClass("hide");
	    $("#getPsdAlert").addClass("hide");
        $(this).css("color", "#374140");
       
    }).blur(function () {
	    VerifyGetPsdUserName();
    });
	
	$("#btn_getPsd").click(function () {
		SubmitGetPsdFrm();
    });
	
	$("#sendOne").click(function () {
		$("#emailType").attr("value", "1");
		SubmitGetPsdFrm2();
    });
	$("#sendOneForC").click(function () {
		$("#emailType").attr("value", "1");
		SubmitGetPsdFrmForC();
    });
	
	$("#sendRegOne").click(function () {
		$("#emailType").attr("value", "2");
		SubmitGetPsdFrm2();
    });
	
	
});

function CloseWin() {
	var ua = navigator.userAgent;
	var ie = navigator.appName == "Microsoft Internet Explorer" ? true : false;
	if (ie) {
		var IEversion = parseFloat(ua.substring(ua.indexOf("MSIE ") + 5, ua.indexOf(";", ua.indexOf("MSIE "))));
		if (IEversion < 5.5) {
			var str = '<object id=noTipClose classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">';
			str += '<param name="Command" value="Close"></object>';
			document.body.insertAdjacentHTML("beforeEnd", str);
			document.all.noTipClose.Click();
		} else {
			window.opener = null;
			window.close();
		}
	} else {
		window.close();
	}
}


